/*
 * @Author: your name
 * @Date: 2021-08-24 15:06:20
 * @LastEditTime: 2021-10-18 20:42:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \bigdate\src\ntils\hooks.ts
 */

import { Menu } from '../router/type'
import { GetLocalStrong } from '../ntils/hooks'
import type { Ref } from 'vue'
import type { EChartsOption } from 'echarts'
import type { ModalFuncProps } from 'ant-design-vue/lib/modal/Modal'
import { message as Message, notification, Modal } from 'ant-design-vue'
import { NotificationArgsProps, ConfigProps } from 'ant-design-vue/lib/notification'
import { unref, nextTick, watch, computed, ref } from 'vue'
import { RequestAxios } from './Request'
import echarts from './echart'
import { AesEncryption } from './cipher'
import moment from 'moment'
// 设置Modal强类型
export interface ModalOptionsEx extends Omit<ModalFuncProps, 'iconType'> {
  iconType: 'warning' | 'success' | 'error' | 'info';
}
export type ModalOptionsPartial = Partial<ModalOptionsEx> & Pick<ModalOptionsEx, 'content'>;
export interface State {
    userinfo:Object,
    routerArr:Menu[] 
}

interface role{
     key:string,
     value:string | Number
}
// 获取本地token
export function GetLocalStrong(key):any {
    return window.localStorage.getItem(key)
}
// 设置本地token
export function SetLocalStrong(info:role) {
    window.localStorage.setItem(info.key, info.value)
}
// 获取浏览器高度
export function useWindowSizeFn():any {
    return window.innerHeight
}
// 清除指定localStorage
export function ClearLocalItem(item:String) {
    window.localStorage.removeItem(item)
}

// 显示提示
export interface NotifyApi {
    info(config: NotificationArgsProps): void;
    success(config: NotificationArgsProps): void;
    error(config: NotificationArgsProps): void;
    warn(config: NotificationArgsProps): void;
    warning(config: NotificationArgsProps): void;
    open(args: NotificationArgsProps): void;
    close(key: String): void;
    config(options: ConfigProps): void;
    destroy(): void;
  }
// 二次封装  Message、notification
export function useMassage() {
    function createModalsuccess(options:ModalOptionsPartial) {
        return Modal.success({ ...options }, 'success')
    }
    function createModalwarning(options:ModalOptionsPartial) {
        return Modal.warning({ ...options }, 'warning')
    }
    function createModalinfo(options:ModalOptionsPartial) {
        return Modal.info({ ...options }, 'info')
    }
    function createModalerror(options:ModalOptionsPartial) {
        return Modal.error({ ...options }, 'error')
    }
    return {
        createMessage: Message,
        createModalwarning,
        createModalsuccess,
        createModalinfo,
        createModalerror,
        notification: notification as NotifyApi,
    }
}

// 获取Moudle下的内容

export function getMassage(langs:Object) {
    let obj = {}
    Object.keys(langs).forEach((key) => {
        let msg = langs[key].default
        Object.assign(obj, msg)
    })
    return obj
}

// echart 封装
export function useECharts(
    elRef: Ref<HTMLDivElement>,
    theme: 'light' | 'dark' | 'default', 
) {
    let chartInstance: echarts.ECharts | null = null
    const cacheOptions = ref({}) 
    const el = unref(elRef)
    const t = theme
    // 图表配置 响应式 cacheOptions
    const getOptions = computed(() => {
        if (theme !== 'dark') {
            return cacheOptions.value as EChartsOption
        }
        return {
            backgroundColor: 'transparent',
            ...cacheOptions.value,
        } as EChartsOption
    })
    
    function setOptions(options:EChartsOption, clear = true) {
        cacheOptions.value = options
        nextTick(() => {
            setTimeout(() => {
                // 初始化 t 为主题 有黑暗主题
                chartInstance = echarts.init(el, t)  
                // 清除之前的动画 为下次更新做准备
                clear && chartInstance?.clear()
                // 更新数据  
                chartInstance?.setOption(unref(getOptions))
            }, 30)
        })
    }
    // 监听主题变化
    watch(
        () => theme,
        () => {
            if (chartInstance) {
                chartInstance.dispose()
                setOptions(cacheOptions.value)
            }
        },
    ) 
    return {
        setOptions,
        echarts,
    }  
}

// axios 二次封装
function Vaxios(baseurl) {
    return new RequestAxios({
        timeout: 2 * 1000,
        baseURL: baseurl,
        headers: { 'Content-Type': 'application/json;charset=UTF-8' },
        requestOptions: {},      
    })
}

// 如果有其他api 基础路由自定义配置
// 可以写入
// export const vbaidu = Vaxios('http://ww.baidu.com/');

// 封装 Base64加密解密、MD5加密解密
let AesOptions = {
    key: '_11111000001111@',
    iv: '@11111000001111_',
}
export const encryption = new AesEncryption(AesOptions)
const oH = 3600 * 1000
const expire = 1
// 加密数据--适用于加密local
export const SetAesEncryLocal = (options) => {
    let str = JSON.stringify({
        value: options, // 初始值
        time: Date.now(), // 设置初始事件
        expire: new Date().getTime() + expire * oH, // 到期事件
    })
    const stringifyValue = encryption.encryptByAES(str)
    return stringifyValue
}
//  解密数据
export const GetAeDecryptLocal = (options) => {
    const stringifyValue = encryption.decryptByAES(options)
    return stringifyValue
}

// 时间戳转化为时间

export const format = (shijianchuo) => {
    let time = new Date(shijianchuo)
    let y = time.getFullYear()
    let m = time.getMonth() + 1
    let d = time.getDate()
    let h = time.getHours()
    let mm = time.getMinutes()
    let s = time.getSeconds()
    return y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
}

//  使用moment格式化时间

export const momentTime = (str)=>{
     return moment().format(str);
}

// 服务器接口
export const serviceApi = Vaxios('http://120.76.142.67:3061/')
// ckeditor 接口
export const gettoken = Vaxios('/upload')